package com.ghck.server.controller;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.GhckAplication;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.Message;
import com.ghck.server.model.response.Result;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.service.MessageService;
import com.ghck.server.utils.AjkUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author chenxuan
 *
 */
@Controller
@RequestMapping(value="/message")
public class MessageController {

	private static Logger logger = LoggerFactory.getLogger(MessageController.class);
	@Autowired
	private MessageService messageService;
	
	@ApiOperation(value="我的消息列表")
	@RequestMapping(value="/my/list" ,method=RequestMethod.GET)
	@PreAuthorize(GhckAplication.all)
	@ResponseBody
	public ResultList<Message> myList(Principal principal,Message message){
		ResultList<Message> result = new ResultList<Message>();
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		message.setUserId(user.getUserId());
		if(message.getStart() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分页参数 start 不能为空");
			throw exception;
		}
		if(message.getLimit() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分页参数 limit 不能为空");
			throw exception;
		}
		List<Message> list = messageService.myList(message);
		result.setItemList(list);
		return result;
	}
	
	@ApiOperation(value="未读消息数")
	@RequestMapping(value="/unread_count",method=RequestMethod.GET)
	@PreAuthorize(GhckAplication.all)
	@ResponseBody
	public ResultObject<Integer> unreadCount(Principal principal){
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		return new ResultObject<Integer>(messageService.unreadCount(user.getUserId()));
	}
	
	@ApiOperation(value="消息标记为已读")
	@RequestMapping(value="/mark_read",method=RequestMethod.PUT)
	@PreAuthorize(GhckAplication.all)
	@ResponseBody
	public Result markAsRead(Principal principal,Message message){
		if(message.getId() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"消息ID不能为空");
			throw exception;
		}
		messageService.markAsRead(message.getId());
		return new Result();
	}
	
	@ApiOperation(value="消息详情")
	@RequestMapping(value="/detail/{id}/{rid}")
	public String detail(@PathVariable("id") Integer id,@PathVariable("rid") Integer rid,HttpServletRequest request){
		messageService.markAsRead(rid);
		Message message =  messageService.detail(id);
		request.setAttribute("message", message);
		return "message/detail";
	}
	
}